package terminal.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import terminal.communication.CommunicationService;
import terminal.communication.CommunicationServiceImpl;
import terminal.communication.InvocationHandler;
import terminal.communication.InvocationHandlerImpl;
import terminal.communication.MessageReceiver;
import terminal.communication.MessageSender;
import terminal.communication.Server;
import terminal.gui.MainWindow;
import terminal.utils.BeanProvider;
import core.smartCardReader.SmartCardReaderService;

@Configuration
public class ApplicationConfig {
	@Autowired
	private Server server;

	@Autowired
	private MessageSender messageSender;

	@Autowired
	private MessageReceiver messageReceiver;

	@Autowired
	private CommunicationService communicationService;

	@Autowired
	private InvocationHandler invocationHandler;

	@Autowired
	private SmartCardReaderService smartCardReaderService;

	@Autowired
	private BeanProvider beanProvider;

	@Autowired
	private MainWindow mainWindow;

	@Bean
	public MainWindow mainWindow() {
		return new MainWindow();
	}

	@Bean
	public CommunicationService communicationService() {
		return new CommunicationServiceImpl(server, messageSender, messageReceiver, taskExecutor());
	}

	@Bean
	public InvocationHandler invocationHandler() {
		return new InvocationHandlerImpl(communicationService);
	}

	@Bean
	public Server server() {
		return new Server();
	}

	@Bean
	public MessageSender messageSender() {
		return new MessageSender();
	}

	@Bean
	public MessageReceiver messageReceiver() {
		return new MessageReceiver();
	}

	@Bean
	public ThreadPoolTaskExecutor taskExecutor() {
		ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
		pool.setCorePoolSize(5);
		pool.setMaxPoolSize(10);
		pool.setWaitForTasksToCompleteOnShutdown(true);
		return pool;
	}

	@Bean
	public BeanProvider beanProvider() {
		return new BeanProvider();
	}

	@Bean
	public SmartCardReaderService smartCardReaderService() {
		return new SmartCardReaderService();
	}
}
